  package com.nbst.service.base.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import com.nbst.comnutil.LogOut;
import com.nbst.comnutil.NormalResult;
import com.nbst.dao.mapper.mes.DepartmentMapper;
import com.nbst.dao.mapper.mes.UserMapper;
import com.nbst.model.base.Base;
import com.nbst.model.base.Department;
import com.nbst.service.base.IDepartmentService;

/**
 * @ClassName: DepartmentServiceImpl
 * @Description:部门管理Service实现层
 * @author 兵
 * @date 2017-8-15
 * 
 */
@Service
public class DepartmentServiceImpl implements IDepartmentService {
	@Autowired
	DepartmentMapper departmentmapper;

	@Autowired
	LogOut logout;

	@Autowired
	UserMapper usermapper;

	@Override
	public NormalResult deleteByPrimaryKey(String dId) {
		NormalResult result = new NormalResult();
		int num = 0;
		try {
			num = departmentmapper.deleteByPrimaryKey(dId);
			if (num == 1) {
				result.setCode("0000");
				result.setMessage("删除成功");
			} else {
				result.setCode("9999");
				result.setMessage("删除失败");
			}
		} catch (Exception e) {
			logout.error(e);
			result.setDataset(e);
		}
		return result;
	}

	@Override
	@Transactional
	public NormalResult insertSelective(Department record) {
		NormalResult result = new NormalResult();
		if (StringUtils.isEmpty(record.getfName())) {
			result.setCode("9999");
			result.setMessage("该部门名称不能为空");
			return result;
		}
		Map<String,Object> map = new HashMap<>(5);
		map.put("fName", record.getfName());
		List<Department> list1 = departmentmapper.findByCondition(map);
		if (list1.size() > 0) {
			result.setCode("9999");
			result.setMessage("该部门名称重复");
			return result;
		}
		record.setfDate(new Date());
		int num = 0;
		try {
			num = departmentmapper.insertSelective(record);
			if (num == 1) {
				result.setCode("0000");
				result.setMessage("新增成功");
				result.setId(record.getId().toString());
			} else {
				result.setCode("9999");
				result.setMessage("新增失败");
			}
		} catch (Exception e) {
			logout.error(e);
			result.setDataset(e);
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚
		}
		return result;
	}

	@Override
	public List<Department> findByCondition(Map<String,Object> map) {
		List<Department> list = new ArrayList<Department>();
		try {
			list = departmentmapper.findByCondition(map);

		} catch (Exception e) {
			logout.error(e);

		}
		return list;
	}

	@Override
	public NormalResult updateByPrimaryKeySelective(Department record) {
		NormalResult result = new NormalResult();
		if (record.getfClosed().intValue()==0) {
			record.setfUpdateTime(new Date());
			Map<String,Object> map = new HashMap<>();
			map.put("fDepId", record.getId());
			List list = usermapper.findByCondition(map);
			if (!list.isEmpty()) {
				result.setCode("9999");
				result.setMessage("该部门下还有用户挂靠，不能删除");
				return result;
			}
			map.put("dId", record.getId());
			map.put("depId", record.getId());
			
		} else {
			record.setfUpdateTime(new Date());
		}
		int num = 0;
		try {
			num = departmentmapper.updateByPrimaryKeySelective(record);
			if (num == 1) {
				result.setCode("0000");
				if (record.getfClosed().intValue()==0) {
					result.setMessage("删除成功");
				} else {
					result.setMessage("修改成功");
				}
			} else {
				result.setCode("9999");
				if (record.getfClosed().intValue()==0) {
					result.setMessage("删除失败");
				} else {
					result.setMessage("修改失败");
				}
			}
		} catch (Exception e) {
			logout.error(e);
			result.setCode("9999");
			result.setMessage("出错了,刷新后再试试吧");
			result.setDataset(e);
		}
		return result;
	}


	@Override
	public int count(Map<String,Object> map) {
		int total = 0;
		try {
			total = departmentmapper.count(map);
		} catch (Exception e) {
			logout.error(e);
		}
		return total;
	}

	@Override
	public List<Base> getDepartmentTree(Map<String,Object> map) {
		List<Base> list = new ArrayList<Base>();
		try {
			list = departmentmapper.getDeptAndWorkShopTree();
		} catch (Exception e) {
			logout.error(e);
		}
		return list;
	}

	@Override
	public List<Map<String,Object>> getKeyAndValueList(Map<String,Object> map) {
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = departmentmapper.getKeyAndValueList(map);
		} catch (Exception e) {
			logout.error(e);
		}
		return list;
	}

	@Override
	public List getDeptAndWorkShopTree() {
		return departmentmapper.getDeptAndWorkShopTree();
	}

}